<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
define('IN_ECS', true);
require('../includes/init.php');
include_once('../comm.php');
//include_once('thumb.php');
$FMUID = $_SESSION['user_id'];

if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $png = $GLOBALS['HTTP_RAW_POST_DATA'];
    $size = isset($_GET['size'])?$_GET['size']:'';
    $goods_id = isset($_GET['goods_id'])?$_GET['goods_id']:0;
        
    if($FMUID!=0){
    	$path = '../tmp/'.$FMUID.time().'.png';
  	}else{
  		$path = '../tmp/515415'.time().'.png';
  	}
    
    //文件是否可写
    $fhandle = $path;
    if(!$handle = fopen($fhandle,'wb')){
        echo "不能打开文件   $fhandle";
        echo 'FALSE';
        exit;
    }
    //   将$somecontent写入到打开的文件中。  
    if(!fwrite($handle,$png)){
        echo "不能写入到文件   $fhandle";  
        echo 'FALSE';
        exit;
    }
    //print "成功地将   $somecontent   写入到文件$filename";  
    fclose($handle);

system('convert '.$path.' -units "PixelsPerInch" -density 400 -resample "400x" '.$path);


    //取图片真实宽高
    $img = NewMagickWand();
    MagickReadImage ( $img, $path );
    $h = MagickGetImageHeight($img);
    $w = MagickGetImageWidth($img);
		
		
			
		/*
    // 新图片
    $dst = NewMagickWand();	
    MagickNewImage($dst, $w, $h);
    
    MagickAddImage( $dst, $img );
    MagickSetFormat($dst, 'PNG');
    if($FMUID!=0){
    	$path = '../tmp/'.$FMUID.time().'.png';
  	}else{
  		$path = '../tmp/515415'.time().'.png';
  	}
    MagickWriteImage($dst, $path);
		DestroyMagickWand($dst);
	  */


    //称动文件到maked/uid/images目录
    //目标路径
    $fn = time(); //文件名，不包含扩展名。
    $filename = $fn.'.png'; //文件名，包含扩展名。
    
    $dir1 = '/uploads/maked/'.$FMUID.'/images/';  
    $dir1thumb = '/uploads/maked/'.$FMUID.'/thumbs/';
    
    $path1 = $fm_rootdir.$dir1; //作品目录
    $paththumb = $fm_rootdir.$dir1thumb; //作品缩略图目录
    
	$dir11 = '../uploads/maked/'.$FMUID.'/';
	if(!is_dir($dir11)){
    	mkdir($dir11);
	}
    
    // 用户目录不存在则创建
    $dir2 = '..'.$dir1; //作品
    if(!is_dir($dir2)){
        mkdir($dir2);
    }
    $dir3 = '..'.$dir1thumb; //缩略图
    if(!is_dir($dir3)){
        mkdir($dir3);
    }

    $f1 = $dir1.$filename; //文件名和路径, 存入数据库的。
    $f2 = $dir1thumb.$filename;//同上。缩略图。
    
    $target_file = $path1.$filename; //目标文件，包括路径。
    //保存作品
    if(copy($path, $target_file)){
        $result = "saved";
    }else{
        $result = 'copy file failure';
        exit;
    }
    
    
    //生成缩略图
    $thumbfile = $paththumb.$filename;
    if($h>80 || $w>80){
        $width = 80;
        $height = 80;
        $xratio = $width/$w;
        $yratio = $height/$h;
        
        if($h < $w) {
            $height = floor($h*$xratio);
        }else{
            $width = floor($w*$yratio);
        }
        
        //去掉颜色配置等注释信息
        MagickRemoveImageProfiles($img);
        //使用PointFilter方式缩放到5倍的目标大小
        //MagickSampleImage($img,$width,$height);
        //Resize到目标尺寸,同时锐化处理
        MagickResizeImage($img,$width,$height,MW_PointFilter,0.45);
        MagickSetImageFormat($img,'image/png'); //设置文件类型
        $ok = MagickWriteImage($img,$thumbfile);
        DestroyMagickWand($img);
        if(!$ok){
            echo '生成缩略图失败';
            exit; 
        }
        
    }else{
        //保存缩略图
        if(copy($path, $thumbfile)){
            $result = "saved";
        }else{
            $result = 'copy file failure';
            exit;
        }
    }
    
    
    
    //文件信息入数据库
    $query = "INSERT INTO `fmmaked`(uid,url,memo,width,height,aid,thumb,size,goods_id)VALUES('$FMUID','$f1','$fn','$w','$h', '$idx', '$f2', '$size', '$goods_id')";
    $re = $GLOBALS['db']->query($query);
    if($re){
        $result = "success";
    }else{
        $result = "write into database is failure";
        exit;
    }
    

		$d = "<root><info><result>OK</result><file>".$query."</file></info></root>";	
		echo $d;
		exit;



}else {
    echo "http data FALSE";
}

















$d = "<root><info><result>".$result."</result></info></root>";
echo $d;
?>
